<?php

header("content-type:text/html;charset=utf-8");

require_once "../../php/config/database.php";
require_once "userDb.php";
$UserDb=new UserDb();
$id=$_GET['id'];
$addtime=date("Y-m-d H:i:s",time());

//1.通过$_FILES文件上传变量接收上传文件信息
$fileInfo=$_FILES['myFile'];
$filename=$fileInfo['name'];
$type=$fileInfo['type'];
$tmp_name=$fileInfo['tmp_name'];
$size=$fileInfo['size'];
$error=$fileInfo['error'];
$date=time();
$filename=explode(".",$filename);
$filename=".".$filename[1];
//2.判断下错误号，只有为0或者是UPLOAD_ERR_OK，没有错误发生，上传成功
if($error==UPLOAD_ERR_OK){
    if(move_uploaded_file($tmp_name, "../../upload/userfile/".$id.$date.$filename)){
        $url="https://www.wangjibu.top/weboffice/upload/userfile/".$id.$date.$filename;
         $file=$url;
        $data=$UserDb->updateUpload($id,$file,$addtime);

        $dat = array(

            'src'=>$url,
            'message' =>"上传成功",

        );
        echo json_encode($dat);
    }else{
        $dat = array(

            'message' =>"上传到服务器失败",

        );
        echo json_encode($dat);
    }
}else{
    //匹配错误信息
    switch($error){
        case 1:
            echo '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
            break;
        case 2:
            echo '超过了表单MAX_FILE_SIZE限制的大小';
            break;
        case 3:
            echo '文件部分被上传';
            break;
        case 4:
            echo '没有选择上传文件';
            break;
        case 6:
            echo '没有找到临时目录';
            break;
        case 7:
        case 8:
            echo '系统错误';
            break;
    }
}